<?php

namespace app\dianli\controller;

use app\dianli\model\AreasModel;
use app\dianli\model\CustomerNatureModel;
use app\dianli\model\DianliShoudianModel;
use app\resource\model\CurrencyModel;
use cmf\controller\AdminBaseController;

class ShoudianController extends AdminBaseController
{
    /**
     * 售电列表
     * @adminMenu(
     *     'name'   => '售电列表',
     *     'parent' => 'business/LicensePlate/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 100,
     *     'icon'   => '',
     *     'remark' => '售电列表',
     *     'param'  => ''
     * )
     */
    function index()
    {
        $shoudianModel = new DianliShoudianModel();

        $where = [];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['shoudian.uid'] = ['in', $users];
        }

        if (input('keyword')) {
            $keyword = input('keyword');
            $where['shoudian.company|shoudian.corporation|phone|email|cnature.name|user.user_nickname'] = ['like', "%$keyword%"];
        }

        if (input('publicity',0,'intval')) {
            $where['shoudian.publicity'] = input('publicity',0,'intval');
        }

        $lists = $shoudianModel
            ->alias('shoudian')
            ->join('__AREAS__ areas','shoudian.area = areas.areaid')
            ->join('__CUSTOMER_NATURE__ cnature','shoudian.publicity = cnature.id and cnature.type = 4','left')
            ->join('__USER__ user','shoudian.uid = user.id')
            ->field('shoudian.*,areas.name aname,cnature.name cname,user.user_nickname')
            ->where($where)
            ->order('shoudian.id desc')
            ->paginate();

        $customerNatureModel = new CustomerNatureModel();
        $publicity = $customerNatureModel->where([
            'type' => 4,
            'is_del' => 0
        ])->field('id,name')->select();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $this->assign('where_r', $where_r);
        $this->assign('lists', $lists);
        $this->assign('publicity', $publicity);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * 添加
     * @adminMenu(
     *     'name'   => '添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加',
     *     'param'  => ''
     * )
     */
    function add()
    {
        if ($this->request->isPost()) {
            $arrData = $this->request->post();

            $shoudianModel = new DianliShoudianModel();
            $result = $shoudianModel->allowField(true)->validate(true)->save($arrData);
            if ($result) {
                $this->success('添加成功', url('index'));
            } else {
                $this->error($shoudianModel->getError());
            }
        }
        $currency = new CurrencyModel();
        $defCurrency = $currency->where(['id' => 1])->find();

        $areas = new AreasModel();
        $areaslist = $areas->where(['parentid' => 0])->order('sort')->select()->toArray();

        $this->assign('defCurrency', $defCurrency);
        $this->assign('areaslist', $areaslist);
        return $this->fetch();
    }

    /**
     * 编辑
     * @adminMenu(
     *     'name'   => '编辑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑',
     *     'param'  => ''
     * )
     */
    function edit()
    {
        if ($this->request->isPost()) {
            $arrData = $this->request->post();
            if (empty($arrData['id'])) {
                $this->error('参数错误');
            }

            $shoudianModel = new DianliShoudianModel();
            $result = $shoudianModel->allowField(true)->validate(true)->save($arrData,[
                'id' => $arrData['id']
            ]);
            if ($result) {
                $this->success('保存成功', url('index'));
            } else {
                $this->error($shoudianModel->getError());
            }
        }
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            $this->error('参数错误');
        }
        $shoudianModel = new DianliShoudianModel();
        $result = $shoudianModel
            ->alias('shoudian')
            ->join('__CURRENCY__ currency', 'shoudian.currency = currency.id')
            ->join('__CUSTOMER_NATURE__ cnature', 'shoudian.publicity = cnature.id')
            ->field('shoudian.*,currency.name cname,cnature.name bname')
            ->find($id);

        $areas = new AreasModel();
        $areaslist = $areas->where(['parentid' => 0])->order('sort')->select()->toArray();

        $this->assign('result', $result);
        $this->assign('areaslist', $areaslist);
        return $this->fetch();
    }
}